package org.jimthompson.delinkious;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Params
{
	public Params(boolean blogprobe_only)
	{
		Properties prop = loadProperties();
		
		boolean properr = false;
		
		
		// Load the property for the blog user name.
		p_blog_username = prop.getProperty("blog-username");

		if (p_blog_username == null)
		{
			System.err.println("*** Error: no property blog-username.");
			properr = true;
		}
		

		// Load the property for the blog URL.
		p_blog_url = prop.getProperty("blog-url");

		if (p_blog_url == null)
		{
			System.err.println("*** Error: no property blog-url.");
			properr = true;
		}
		
		
		// Load the property for the blog password.
		p_blog_password = prop.getProperty("blog-password");

		if (p_blog_password == null)
		{
			System.err.println("*** Error: no property blog-password.");
			properr = true;
		}
        
        // If we're loading properties just to probe the blog, then test the
        // needed properties now and bail.
        if (blogprobe_only)
        {
            if (properr)
            {
                System.err.println("Cannot continue without properties. Bye!");
                System.exit(1);
            }
            
            return;
        }

		// Load the property for the blog ID.
		p_blog_id = prop.getProperty("blog-id");

		if (p_blog_id == null)
		{
			System.err.println("*** Error: no property blog-id.");
			properr = true;
		}
		

        // Load the property for the del.icio.us user name.
        p_delicious_username = prop.getProperty("delicious-username");
        
        if (p_delicious_username == null)
        {
            System.err.println("*** Error: no property delicious-username.");
            properr = true;
        }
        
        
        // Load the property for the del.icio.us password.
        p_delicious_password = prop.getProperty("delicious-password");
        
        if (p_delicious_password == null)
        {
            System.err.println("*** Error: no property delicious-password.");
            properr = true;
        }
        
        
		if (properr)
		{
			System.err.println("Cannot continue without properties. Bye!");
			System.exit(1);
		}
	}
	
	static private Properties loadProperties()
	{
		Properties p = new Properties();
        boolean properr = false;

		String home = System.getProperty("user.home");
		String slash = System.getProperty("file.separator");
        String filename = home + slash + "foo.properties";
		
		InputStream in;
		try
		{
			in = new FileInputStream(filename);
			p.load(in);
		} 
		catch (FileNotFoundException e) 
		{
			System.err.println("*** Error: unable to find properties file.");
            System.err.println("           " + filename);
            properr = true;
			//e.printStackTrace();
		} 
		catch (IOException e) 
		{
			System.err.println("*** Error: unable to load properties file.");
            properr = true;
			//e.printStackTrace();
		}
        
        if (properr)
        {
            System.err.println("Cannot continue without properties. Bye!");
            System.exit(1);
        }
        		
		return p;
	}

	String 		p_delicious_username = null;
	String 		p_delicious_password = null;

	String		p_blog_url = null;
	String		p_blog_username = null;
	String		p_blog_password = null; 
	String		p_blog_id = null;
}
